package com.zhouyiquan.qqclient.view;

import com.zhouyiquan.qqclient.service.MessageClientService;
import com.zhouyiquan.qqclient.service.UserClientService;

import java.util.Scanner;

/**
 * @Author: 周益全 (https://gitee.com/zfovikqdev928)
 * @BugFeedBackEmail: zfovikqdev928@126.com
 * @Date: Create in 2023-01
 * @Description: 客户端菜单界面
 * @Version:
 */
public class QQView {
    private final Scanner scanner = new Scanner(System.in);
    private final UserClientService userClientService = new UserClientService(); // 用于登录服务器 / 注册用户
    private final MessageClientService messageClientService = new MessageClientService(); // 私聊 / 群聊
    private boolean loop = true; // 是否显示菜单
    private String key = "";    // 接受用户的键盘输入

    public static void main(String[] args) {
        new QQView().mainMenu();
        System.out.println("客户端退出系统...");
        // System.exit(0); // 正常退出，直接把进程挂掉，线程也会挂掉，就不会造成阻塞情况发生
    }

    // 显示主菜单
    private void mainMenu() {

        while (loop) {
            System.out.println("=====欢迎登录网络通讯系统=====");
            System.out.println("| \t\t 1 登录系统 \t\t |");
            System.out.println("| \t\t 9 退出系统 \t\t |");
            System.out.println("==========================");
            System.out.print("请输入：");
            key = scanner.nextLine();
            switch (key) {
                case "1":
                    System.out.print("请输入用户号：");
                    String userId = scanner.nextLine();
                    System.out.print("请输入密 码：");
                    String passwd = scanner.nextLine();
                   
                    // 请求服务器进行用户账户密码校验
                    // 用户登录成功
                    if (userClientService.checkUser(userId, passwd)) {
                        System.out.println("=====欢迎(用户" + userId + "登录成功）=====");
                        // 进入二级菜单
                        while (loop) {
                            System.out.println("\n ============= 网络通信系统二级菜单（用户" + userId + ")=============");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.print("请输入你的选择：");
                            key = scanner.nextLine();
                            switch (key) {
                                case "1":
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.println("请输入想对大家说的话：");
                                    String talkAll = scanner.nextLine();
                                    messageClientService.sendMessageToAll(talkAll, userId);
                                    break;
                                case "3":
                                    System.out.println("请输入想聊天的用户号(在线)：");
                                    String receiver = scanner.nextLine();
                                    System.out.println("请输入想说的话：");
                                    String chatContent = scanner.nextLine();
                                    messageClientService.sendMessageToOne(chatContent, userId, receiver);   
                                    break;
                                case "4":
                                    break;
                                case "9":
                                    userClientService.logout();
                                    loop = false;
                                    break;
                            }
                        }
                    } else { // 用户登录失败
                        System.out.println("登陆失败！！！");
                    }
                    break;
                case "9":
                    System.out.println("退出");
                    loop = false;
                    break;
            }
        }
    }
}
